<?php


namespace app\common\model;


use think\Db;
use think\Exception;
use think\Model;

class UserWithdrawalLog extends Model
{
    use \traits\model\SoftDelete;

    // 表名
    protected $name = 'user_withdrawal_log';


    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = 'delete_dt';

    // 追加属性
    protected $append = [
        'log_status_text'
    ];

    public function getLogStatusList()
    {
        return ['-1' => '不通过', '0' => '审核中', '1' => '通过'];
    }


    public function getLogStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['log_status']) ? $data['log_status'] : '');
        $list = $this->getLogStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('\app\admin\model\Admin', 'handel_admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    /**
     * 申请提现
     * @param object $user 提现商家
     * @param float $wallet 提现金额
     * @param array $extend 其他
     * @return bool
     */
    public static function addLog($user, $wallet, $extend = [])
    {
        Db::startTrans();
        try {
            $beforeWallet = $user->money;
            $afterWallet = bcsub($user->money, $wallet,2);
            $obj = new self();
            $data = [
                'user_id' => $user->id,
                'wallet' => $wallet,
                'before_wallet' => $beforeWallet,
                'after_wallet' => $afterWallet,
                'with_time' => time(),
                'with_desc' => $extend['with_desc'] ?? '',
                'withdrawal_no' => $extend['withdrawal_no'] ?? '',
                'log_status' => 0,
                'log_type' =>2,
                'card_no' => $extend['card_no'],
            ];
            $obj->save($data);//创建提现记录

            //变动账户金额和冻结金额
            User::money($wallet, $user->id, '提现', $obj->id, 1);

            Db::commit();
            return true;
        } catch (Exception $exception) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 通过提现申请
     * @param object $withdrawal 提现记录对象
     * @param array $extend 其他
     * @return bool
     */
    public function pass($withdrawal, $extend = [])
    {
        Db::startTrans();
        try {
            $withdrawal = is_numeric($withdrawal) ? $this->where('id', $withdrawal)->find() : $withdrawal;
            $withdrawal->log_status = 1;
            $withdrawal->handel_time = time();
            if (isset($extend['handel_admin_id'])) $withdrawal->handel_admin_id = $extend['handel_admin_id'];
            if (isset($extend['handel_desc'])) $withdrawal->handel_desc = $extend['handel_desc'];
            $withdrawal->isUpdate(true)->save();
            //变动账户金额和冻结金额
            $wallet = bcmul($withdrawal->wallet, -1);
            User::money($wallet, $withdrawal->user_id, '提现通过', $withdrawal->id, 1);
            Db::commit();
            return true;
        } catch (Exception $exception) {
            Db::rollback();
            return false;
        }
    }

    public function reject($withdrawal, $extend = [])
    {
        Db::startTrans();
        try {
            $withdrawal = is_numeric($withdrawal) ? $this->where('id', $withdrawal)->find() : $withdrawal;
            $withdrawal->log_status = -1;
            $withdrawal->handel_time = time();
            if (isset($extend['handel_admin_id'])) $withdrawal->handel_admin_id = $extend['handel_admin_id'];
            if (isset($extend['handel_desc'])) $withdrawal->handel_desc = $extend['handel_desc'];
            $withdrawal->isUpdate(true)->save();
            //变动账户金额和冻结金额
            $wallet = bcmul($withdrawal->wallet, -1);
            User::money($wallet, $withdrawal->user_id, '提现驳回', $withdrawal->id, 1);
            User::money($withdrawal->wallet, $withdrawal->user_id, '提现驳回', $withdrawal->id);
            Db::commit();
            return true;
        } catch (Exception $exception) {
            Db::rollback();
            return false;
        }
    }

}